<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
MYDCONN();
$GLOBALS['MYT']=$GLOBALS['MYD'];
	$SumTotal = 0;
	$tmp = '';
	if ($_POST['RefInvoice']==0) { $tmp = "AND sysUser = ".$_SESSION[$SESSID]." AND TmpInvoice = ".$_POST['RefInvoice'];}
?>
<div style="width:100%;height:360px;overflow:auto">
    <table border="0" cellpadding="2" cellspacing="1" bgcolor="#EAEAEA">
      <tr bgcolor="#F8F8F8">
        <td>Loja</td>
	<td><div id="OfficeDiv"><?php include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/sel_office.php');?></div></td>
        <td>Discritivo</td>
        <td>Tipo</td>
        <td>Quant.</td>
        <td>PreÇo</td>
        <td>Prom.</td>
        <td>IVA %</td>
        <td>Total</td>
        <td rowspan="2" align="center">
        <?php mn_field('button','ED','AddArticle','','','','','insert" onclick="
        		document.edtCdcInvoice.action=\'_response/rsproxy.php?pr=cdc_article\';
        		subform(\'edtCdcInvoice\',\'elem_articles\');'); ?></td>
      </tr>
      <tr bgcolor="#F8F8F8">
	<td>Funcionário</td>
	<td><div id="UserDiv"><?php include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/sel_staff.php');?></div></td>
        <td><?php mn_field('text','ED','ArtDescription','','255','40','',''); ?></td>
        <td><?php mn_field('hselect','ED','ArtTYP',0,"Factura;Nota de Cr&eacutedito#0;1",'','',''); ?></td>
        <td><?php mn_field('text','ED','ArtQuantity','1','4','4','4','INPNUM" onKeyUp="ArticleAmount(this.form);'); ?></td>
        <td><?php mn_field('text','ED','ArtUAmount','0.00','14','8','','INPNUM" onKeyUp="ArticleAmount(this.form);'); ?></td>
        <td><?php mn_field('text','ED','ArtPromo','0.00','6','4','','INPNUM" onKeyUp="ArticleAmount(this.form);'); ?></td>
        <td><?php mn_field('select','ED','ArtTaxPercent',0,"SELECT PERCENT AS ID, PERCENT AS NAME FROM ccenter_gov_tax WHERE STATUS != 5 ORDER BY PERCENT",'','','" onChange="ArticleAmount(this.form);');?></td>
        <td><?php mn_field('text','ED','ArtAmount','0.00','19','10','','INPNUM'); ?></td>
      </tr>
      <?php
	   $List = $GLOBALS['MYD']->db_array("SELECT 
	      			main_users.name AS User,
					main_offices.name AS Office,
	   				ccenter_invarticles.ArtReference,
	   				ccenter_invarticles.Ref,
	   				ccenter_invarticles.ArtReference,
					ccenter_invarticles.Description,
					ccenter_invarticles.Quantity,
					ccenter_invarticles.UAmount,
					ccenter_invarticles.IvaPercent,
					ccenter_invarticles.Promo,
					ccenter_invarticles.Amount
	  			FROM ccenter_invarticles
				LEFT OUTER JOIN main_users ON main_users.ref = ccenter_invarticles.User
				LEFT OUTER JOIN main_offices ON main_offices.ref = ccenter_invarticles.Office
				WHERE RefInvoice = {$_POST['RefInvoice']} {$tmp}
				AND Status != 5");
	for($r = 1; $r <= $List[0]; $r++) {  
     echo '<tr bgcolor="#CAD5DF">
        <td NOWRAP colspan="2">'.$List[$r]['Office'].', '.$List[$r]['User'].'</td>
        <td >'.$List[$r]['Description'].'</td>
        <td >'.$List[$r]['ArtReference'].'</td>
        <td align="right">'.round($List[$r]['Quantity']).'</td>
        <td align="right">'.$List[$r]['UAmount'].'</td>
        <td align="right">'.$List[$r]['Promo'].'</td>
        <td>'.round($List[$r]['IvaPercent']).'%</td>
        <td align="right">'.$List[$r]['Amount'].'</td>
		  <td>';
		  mn_field('button','ED','RemArticle','','','','','delete" onclick="
			document.edtCdcInvoice.action=\'_response/rsproxy.php?pr=cdc_article&ACT=RT&RCINV='.$List[$r]['Ref'].'\';
        		subform(\'edtCdcInvoice\',\'elem_articles\');'); echo '</td>
		</tr>';
	  $SumTotal += $List[$r]['Amount'];
       } ?>
	<tr align="right" bgcolor="#F2F2F2">
    <td colspan="9">
	<?php
	if (!isset($_POST['Amount'])) { $_POST['Amount']= $GET[1]['Amount'];}
	$AmountRes = round($SumTotal,2);
	if ($AmountRes != round($_POST['Amount'],2)) { 
			$Block = 1;
			echo '<font color="#001100">NÃo Coincide</font>';} ?>
	<b>Valor da Factura: &euro;</b><?php mn_field('text','ED','CtrlAmount',number_format($AmountRes,2),'19','10','','INPNUM'); ?></td>
    </tr>
    </table>
</div>